package com.google.code.swingonfire.utility.date;

import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.Locale;

public class DateRenderInfoMonthPart extends AbstractStyledDateRenderInfoPart
{

    public DateRenderInfoMonthPart(int length)
    {
        super(length, determineStyle(length));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getValue(Calendar calendar, DateFormatSymbols dateFormatSymbols, Locale locale)
    {
        int month = calendar.get(Calendar.MONTH);

        switch (getStyle())
        {
            case LONG:
                return dateFormatSymbols.getMonths()[month];

            case MEDIUM:
                return dateFormatSymbols.getShortMonths()[month];

            case SHORT:
                return formatNumber(month + 1);
        }

        throw new IllegalArgumentException("Unsupported style: " + getStyle());
    }

    private static DateRenderInfoStyle determineStyle(int length)
    {
        if (length < 3)
        {
            return DateRenderInfoStyle.SHORT;
        }
        if (length == 3)
        {
            return DateRenderInfoStyle.MEDIUM;
        }

        return DateRenderInfoStyle.LONG;
    }

}
